package com.flyxiang.workflow.service.impl;

import com.flyxiang.common.core.enums.BusinessStatusEnum;
import com.flyxiang.common.core.utils.MapstructUtils;
import com.flyxiang.common.core.utils.StringUtils;
import com.flyxiang.common.mybatis.core.page.TableDataInfo;
import com.flyxiang.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.flyxiang.workflow.domain.bo.WfDynamicDataBo;
import com.flyxiang.workflow.domain.vo.WfDynamicDataVo;
import com.flyxiang.workflow.domain.WfDynamicData;
import com.flyxiang.workflow.mapper.WfDynamicDataMapper;
import com.flyxiang.workflow.service.IWfDynamicDataService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 动态表单数据Service业务层处理
 *
 * @author daojiang
 * @date 2024-11-15
 */
@RequiredArgsConstructor
@Service
public class WfDynamicDataServiceImpl implements IWfDynamicDataService {

    private final WfDynamicDataMapper baseMapper;

    /**
     * 查询动态表单数据
     *
     * @param id 主键
     * @return 动态表单数据
     */
    @Override
    public WfDynamicDataVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询动态表单数据列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 动态表单数据分页列表
     */
    @Override
    public TableDataInfo<WfDynamicDataVo> queryPageList(WfDynamicDataBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WfDynamicData> lqw = buildQueryWrapper(bo);
        Page<WfDynamicDataVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的动态表单数据列表
     *
     * @param bo 查询条件
     * @return 动态表单数据列表
     */
    @Override
    public List<WfDynamicDataVo> queryList(WfDynamicDataBo bo) {
        LambdaQueryWrapper<WfDynamicData> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WfDynamicData> buildQueryWrapper(WfDynamicDataBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WfDynamicData> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), WfDynamicData::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增动态表单数据
     *
     * @param bo 动态表单数据
     * @return 是否新增成功
     */
    @Override
    public WfDynamicDataVo insertByBo(WfDynamicDataBo bo) {
        WfDynamicData add = MapstructUtils.convert(bo, WfDynamicData.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        WfDynamicDataVo wfDynamicDataVo = baseMapper.selectVoById(add.getId());

        return wfDynamicDataVo;
    }

    /**
     * 修改动态表单数据
     *
     * @param bo 动态表单数据
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(WfDynamicDataBo bo) {
        WfDynamicData update = MapstructUtils.convert(bo, WfDynamicData.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WfDynamicData entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除动态表单数据信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 更新业务表的 业务状态
     *
     * @param key         流程key(流程定义的key)
     * @param businessKey wf_dynamic_data表的主键
     * @param status      业务状态
     */
    @Override
    public void updateServiceStatus(String key, String businessKey, String status) {
        if (StringUtils.isEmpty(status) || StringUtils.isEmpty(businessKey)) {
            return;
        }
        if (!status.equals(BusinessStatusEnum.TERMINATION.getStatus())
            && !status.equals(BusinessStatusEnum.BACK.getStatus())
            && !status.equals(BusinessStatusEnum.INVALID.getStatus())
        ) {
            //状态不是 终止 或 已退回 或 已作废 则不用更新业务表的状态
            return;
        }

        /**
         * 根据主键更新状态
         */
        WfDynamicData dynamicData = new WfDynamicData();
        dynamicData.setId(Long.parseLong(businessKey));
        dynamicData.setStatus(status);
        baseMapper.updateById(dynamicData);

    }

}
